package com.eregg.learn.io;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class IOServer {
    public static void main(String args[]) throws IOException{

        ServerSocket serverSocket = new ServerSocket(8000);

        //1创建一个线程用来接收客户端请求
        new Thread(() -> {

            while (true){
                try {
                    //1阻塞方法获取一个新连接
                    Socket socket = serverSocket.accept();
                    //每一个新的连接都创建一个线程负责读取数据
                    new Thread(() -> {
                        try {
                            int len;
                            byte[] data = new byte[1024];
                            InputStream input = socket.getInputStream();
                            while ((len = input.read(data)) != -1) {

                                System.out.println(Thread.currentThread().getName() + ":" +new String(data, 0, len));
                            }
                        } catch (IOException e) {

                        }

                    }).start();
                } catch (IOException e) {

                }
            }

        }).start();
    }
}
